package com.example.demo.serviceimpl;

import com.example.demo.mapper.*;
import com.example.demo.model.*;
import com.example.demo.service.AdminService;
import com.example.demo.util.FileUtil;
import com.example.demo.util.ImgUtil;
import com.example.demo.util.MD5Util;
import com.example.demo.util.TokenSign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.converter.json.GsonBuilderUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    TYPEMapper typeMapper;
    @Resource
    BELONGMapper belongMapper;
    @Resource
    TEXTMapper textMapper;
    @Resource
    MESSAGEMapper messageMapper;
    @Resource
    USERMapper userMapper;
    @Resource
    IMGMapper imgMapper;
    @Resource
    COOPCOMPANYMapper coopcompanyMapper;
    @Resource
    ADVERTISEMapper advertiseMapper;

    private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    private static MD5Util md5Util=new MD5Util();


    private static String path="D:/dhcProject/images/";

//    @Caching(evict={@CacheEvict(value="typecache",allEntries=true)})
    @Override
    public ResponseBo addType(TYPE type) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addType 方法");
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        type.setCreatetime(datetime);
        type.setUpdatetime(datetime);
        int res=typeMapper.insert(type);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addType 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addType 方法");
        return responseBo;
    }

//    @Caching(evict={@CacheEvict(value="typecache",allEntries=true)})
    @Override
    public ResponseBo delType(int typeid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delType 方法");
        int res=typeMapper.deleteByPrimaryKey(typeid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delType 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delType 方法");
        return responseBo;
    }

//    @Caching(evict={@CacheEvict(value="typecache",allEntries=true)})
    @Override
    public ResponseBo updateType(TYPE type) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateType 方法");
        TYPE dataType=typeMapper.selectByPrimaryKey(type.getId());
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        type.setCreatetime(dataType.getCreatetime());
        type.setUpdatetime(datetime);
        int res=typeMapper.updateByPrimaryKey(type);
        if (res==0){
            responseBo.setResMsg("更新失败");
            log.info("退出 AdminServiceImpl 类的 updateType 方法");
            return responseBo;
        }
        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateType 方法");
        return responseBo;
    }

//    @Cacheable(value = "typecache")
    @Override
    public ResponseBo getTypes() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getTypes 方法");
        List list=typeMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getTypes 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getTypes 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getTypeByBelong(int belong) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getTypeByBelong 方法");
        List list=typeMapper.selectByBelong(belong);
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getTypeByBelong 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getTypeByBelong 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getBelongs() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getBelongs 方法");
        List list=belongMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getBelongs 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getBelongs 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addBelong(BELONG belong) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addBelongs 方法");
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        belong.setCreatetime(datetime);
        int res=belongMapper.insert(belong);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addBelongs 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addBelongs 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delBelong(int belid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delBelong 方法");
        int res=belongMapper.deleteByPrimaryKey(belid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delBelong 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delBelong 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateBelong(BELONG belong) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateBelong 方法");
        BELONG belongDB=belongMapper.selectByPrimaryKey(belong.getId());
        belong.setCreatetime(belongDB.getCreatetime());
        belong.setUpdatetime(simpleDateFormat.format(new Date()));
        int res=belongMapper.updateByPrimaryKey(belong);
        if (res==0){
            responseBo.setResMsg("更新失败");
            log.info("退出 AdminServiceImpl 类的 updateBelong 方法");
            return responseBo;
        }
        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateBelong 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getTexts() throws IOException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getTexts 方法");
        List list=textMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getTexts 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        for (int i=0;i<list.size();i++){
            TEXT temp= (TEXT) list.get(i);
            temp.setBase64img(ImgUtil.imgToBase64(temp.getImgname()));
        }
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getTexts 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addText(TEXT text, MultipartFile file) throws IOException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addText 方法");
        StringBuilder newName=new StringBuilder("aa.jpg");
        int flag=0;
        if(file==null){
            flag=-1;
        }else {
            int res=FileUtil.upload(file,newName,path);
            if (res==0){
                responseBo.setResMsg("图片类型不正确");
                return responseBo;
            }
            if (res==-1){
                responseBo.setResMsg("图片上传出现异常");
                return responseBo;
            }
        }

        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        if (flag!=-1) {
            IMG img = new IMG();
            img.setCreatetime(datetime);
            img.setName(newName.toString());
            img.setUserid(text.getUserid());
            imgMapper.insert(img);
            img = imgMapper.selectByName(img.getName());
            text.setImgid(img.getId());
        }else
            text.setImgid(1);

        text.setCreatetime(datetime);
        text.setUpdatetime(datetime);
        int res=textMapper.insert(text);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addText 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addText 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delText(int textid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delText 方法");
        TEXT text=textMapper.selectByPrimaryKey(textid);
        IMG img=imgMapper.selectByPrimaryKey(text.getImgid());
        int res=textMapper.deleteByPrimaryKey(textid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delText 方法");
            return responseBo;
        }
        if (!img.getName().equals("aa.jpg")) {
            res = FileUtil.delete(img.getName(), path);
            if (res == -1) {
                responseBo.setResMsg("图片删除失败");
                return responseBo;
            }
            res = imgMapper.deleteByPrimaryKey(img.getId());
            if (res == 0) {
                responseBo.setResMsg("图片记录删除失败");
                return responseBo;
            }
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delText 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateText(TEXT text,MultipartFile file) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateText 方法");
        TEXT textDB=textMapper.selectByPrimaryKey(text.getId());
        IMG img=imgMapper.selectByPrimaryKey(textDB.getImgid());
        String imgName=img.getName();
        StringBuilder newName=new StringBuilder("aa.jpg");
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);

        text.setCreatetime(textDB.getCreatetime());
        text.setUpdatetime(datetime);

        if (file!=null){
            int res=FileUtil.upload(file,newName,path);
            if (res==-1){
                responseBo.setResMsg("图片上传失败");
                return responseBo;
            }
            if (res==0){
                responseBo.setResMsg("图片类型不正确");
                return responseBo;
            }
            IMG newImg=new IMG();
            newImg.setUserid(text.getUserid());
            newImg.setName(newName.toString());
            newImg.setCreatetime(datetime);
            imgMapper.insert(newImg);
            newImg=imgMapper.selectByName(newName.toString());
            text.setImgid(newImg.getId());

            res=textMapper.updateByPrimaryKey(text);
            if (res==0){
                responseBo.setResMsg("更新失败");
                log.info("退出 AdminServiceImpl 类的 updateText 方法");
                return responseBo;
            }
            if (!imgName.equals("aa.jpg")) {
                res = FileUtil.delete(imgName, path);
                if (res == -1) {
                    responseBo.setResMsg("图片删除失败");
                    return responseBo;
                }

                res = imgMapper.deleteByPrimaryKey(textDB.getImgid());
                if (res == 0) {
                    responseBo.setResMsg("图片记录删除失败");
                    return responseBo;
                }
            }
        }else{
            text.setImgid(textDB.getImgid());
            int res=textMapper.updateByPrimaryKey(text);
            if (res==0){
                responseBo.setResMsg("更新失败");
                log.info("退出 AdminServiceImpl 类的 updateText 方法");
                return responseBo;
            }
        }



        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateText 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getMessages() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getMessages 方法");
        List list=messageMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getMessages 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getMessages 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addMessage(MESSAGE message) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addMessage 方法");
        int res=messageMapper.insert(message);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addMessage 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addMessage 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delMessage(int mesid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delMessage 方法");
        int res=messageMapper.deleteByPrimaryKey(mesid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delMessage 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delMessage 方法");
        return responseBo;
    }

    @Override
    public ResponseBo login(USER user, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 login 方法");
        USER userDB=userMapper.selectByName(user.getName());
        if (userDB==null){
            responseBo.setResMsg("用户不存在");
            log.info("退出 AdminServiceImpl 类的 login 方法");
            return responseBo;
        }
        if (!md5Util.checkpassword(user.getPassword(),userDB.getPassword())){
            responseBo.setResMsg("密码错误");
            log.info("退出 AdminServiceImpl 类的 login 方法");
            return responseBo;
        }
        userDB.setLogintime(simpleDateFormat.format(new Date()));
        userMapper.updateByPrimaryKey(userDB);

//        HttpSession session = request.getSession();
//        session.setAttribute("userid",userDB.getId());
//        session.setAttribute("tname",userDB.getTname());

        String token= TokenSign.sign(userDB.getName(),String.valueOf(userDB.getId()),userDB.getTname());
        responseBo.setResMsg("登陆成功");
        responseBo.setAccessToken(token);
        responseBo.setResult(userDB);
        log.info("退出 AdminServiceImpl 类的 login 方法");
        return responseBo;
    }

    @Override
    public ResponseBo logout(HttpServletRequest request) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 logout 方法");

//        HttpSession session=request.getSession();
////        session.invalidate();

        responseBo.setResMsg("注销成功");
        log.info("退出 AdminServiceImpl 类的 logout 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getUsers() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getUsers 方法");
        List list=userMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getUsers 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getUsers 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getUserById(int userid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getUserById 方法");
        USER user=userMapper.selectById(userid);
        if (user==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getUserById 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(user);
        log.info("退出 AdminServiceImpl 类的 getUserById 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delUser(int userid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delUser 方法");
        int res=userMapper.deleteByPrimaryKey(userid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delUser 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addUser(USER user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addUser 方法");
        USER userDB=userMapper.selectByName(user.getName());
        if (userDB!=null){
            responseBo.setResMsg("用户名已存在");
            return responseBo;
        }
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        user.setUpdatetime(datetime);
        user.setCreatetime(datetime);
        user.setLogintime(datetime);
        user.setPassword(md5Util.EncoderByMd5(user.getPassword()));
        int res=userMapper.insert(user);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addUser 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateUser(USER user) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateUser 方法");
        USER userDB=userMapper.selectByPrimaryKey(user.getId());
        user.setLogintime(userDB.getLogintime());
        user.setCreatetime(userDB.getCreatetime());
        user.setPassword(userDB.getPassword());
        user.setUpdatetime(simpleDateFormat.format(new Date()));
        int res=userMapper.updateByPrimaryKey(user);
        if (res==0){
            responseBo.setResMsg("更新失败");
            log.info("退出 AdminServiceImpl 类的 updateUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateUser 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateUserPass(USER user) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateUserPass 方法");
        USER userDB=userMapper.selectByPrimaryKey(user.getId());
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        userDB.setUpdatetime(datetime);
        userDB.setPassword(md5Util.EncoderByMd5(user.getPassword()));
        int res=userMapper.updateByPrimaryKey(userDB);
        if (res==0){
            responseBo.setResMsg("更新失败");
            log.info("退出 AdminServiceImpl 类的 updateUserPass 方法");
            return responseBo;
        }
        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateUserPass 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getCoops() throws IOException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getCoops 方法");
        List list=coopcompanyMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getCoops 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        for (int i=0;i<list.size();i++){
            COOPCOMPANY temp= (COOPCOMPANY) list.get(i);
            temp.setBase64img(ImgUtil.imgToBase64(temp.getImgname()));
        }
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getCoops 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delCoop(int coopid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delCoop 方法");
        COOPCOMPANY coopDB=coopcompanyMapper.selectByPrimaryKey(coopid);
        IMG imgDB=imgMapper.selectByPrimaryKey(coopDB.getImgid());
        int res=0;
        if (!imgDB.getName().equals("aa.jpg")) {
            res=imgMapper.deleteByPrimaryKey(coopDB.getImgid());
            if (res==0){
                responseBo.setResMsg("图片记录删除失败");
                log.info("退出 AdminServiceImpl 类的 delCoop 方法");
                return responseBo;
            }

                res = FileUtil.delete(imgDB.getName(), path);
                if (res == -1) {
                    responseBo.setResMsg("图片删除失败");
                    log.info("退出 AdminServiceImpl 类的 delCoop 方法");
                    return responseBo;
                }
        }
        res=coopcompanyMapper.deleteByPrimaryKey(coopid);
        if (res==0){
            responseBo.setResMsg("公司记录删除失败");
            log.info("退出 AdminServiceImpl 类的 delCoop 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delCoop 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateCoop(COOPCOMPANY coop,MultipartFile file,int userid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateCoop 方法");
        COOPCOMPANY coopDB=coopcompanyMapper.selectByPrimaryKey(coop.getId());
        IMG img=imgMapper.selectByPrimaryKey(coopDB.getImgid());
        String imgName=img.getName();
        StringBuilder newName=new StringBuilder("aa.jpg");
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);

        coop.setCreatetime(coopDB.getCreatetime());
        coop.setUpdatetime(datetime);

        if (file!=null){
            int res=FileUtil.upload(file,newName,path);
            if (res==-1){
                responseBo.setResMsg("图片上传失败");
                return responseBo;
            }
            if (res==0){
                responseBo.setResMsg("图片类型不正确");
                return responseBo;
            }
            IMG newImg=new IMG();
            newImg.setUserid(userid);
            newImg.setName(newName.toString());
            newImg.setCreatetime(datetime);
            imgMapper.insert(newImg);
            newImg=imgMapper.selectByName(newName.toString());
            coop.setImgid(newImg.getId());

            res=coopcompanyMapper.updateByPrimaryKey(coop);
            if (res==0){
                responseBo.setResMsg("更新失败");
                log.info("退出 AdminServiceImpl 类的 updateCoop 方法");
                return responseBo;
            }
            if (!imgName.equals("aa.jpg")) {
                res = FileUtil.delete(imgName, path);
                if (res == -1) {
                    responseBo.setResMsg("图片删除失败");
                    return responseBo;
                }

                res = imgMapper.deleteByPrimaryKey(coopDB.getImgid());
                if (res == 0) {
                    responseBo.setResMsg("图片记录删除失败");
                    return responseBo;
                }
            }
        }else{
            coop.setImgid(coopDB.getImgid());
            int res=coopcompanyMapper.updateByPrimaryKey(coop);
            if (res==0){
                responseBo.setResMsg("更新失败");
                log.info("退出 AdminServiceImpl 类的 updateCoop 方法");
                return responseBo;
            }
        }

        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateText 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addCoop(COOPCOMPANY coop, MultipartFile file,int userid) throws IOException {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addCoop 方法");
        StringBuilder newName=new StringBuilder("aa.jpg");
        int flag=0;
        if(file==null){
            flag=-1;
        }else {
            int res=FileUtil.upload(file,newName,path);
            if (res==0){
                responseBo.setResMsg("图片类型不正确");
                return responseBo;
            }
            if (res==-1){
                responseBo.setResMsg("图片上传出现异常");
                return responseBo;
            }
        }

        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        if (flag!=-1) {
            IMG img = new IMG();
            img.setCreatetime(datetime);
            img.setName(newName.toString());
            img.setUserid(userid);
            imgMapper.insert(img);
            img = imgMapper.selectByName(img.getName());
            coop.setImgid(img.getId());
        }else
            coop.setImgid(1);

        coop.setCreatetime(datetime);
        coop.setUpdatetime(datetime);
        int res=coopcompanyMapper.insert(coop);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addCoop 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addCoop 方法");
        return responseBo;
    }

    @Override
    public ResponseBo getAdvs() {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 getUsers 方法");
        List list=advertiseMapper.selectAll();
        if (list==null){
            responseBo.setResMsg("获取失败");
            log.info("退出 AdminServiceImpl 类的 getUsers 方法");
            return responseBo;
        }
        responseBo.setResMsg("获取成功");
        responseBo.setResult(list);
        log.info("退出 AdminServiceImpl 类的 getUsers 方法");
        return responseBo;
    }

    @Override
    public ResponseBo delAdv(int advid) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 delUser 方法");
        int res=advertiseMapper.deleteByPrimaryKey(advid);
        if (res==0){
            responseBo.setResMsg("删除失败");
            log.info("退出 AdminServiceImpl 类的 delUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("删除成功");
        log.info("退出 AdminServiceImpl 类的 delUser 方法");
        return responseBo;
    }

    @Override
    public ResponseBo addAdv(ADVERTISE advertise){
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 addUser 方法");
        Date date=new Date();
        String datetime=simpleDateFormat.format(date);
        advertise.setUpdatetime(datetime);
        advertise.setCreatetime(datetime);
        int res=advertiseMapper.insert(advertise);
        if (res==0){
            responseBo.setResMsg("添加失败");
            log.info("退出 AdminServiceImpl 类的 addUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("添加成功");
        log.info("退出 AdminServiceImpl 类的 addUser 方法");
        return responseBo;
    }

    @Override
    public ResponseBo updateAdv(ADVERTISE advertise) {
        ResponseBo responseBo=new ResponseBo();
        log.info("进入 AdminServiceImpl 类的 updateUser 方法");
        ADVERTISE advertiseDB=advertiseMapper.selectByPrimaryKey(advertise.getId());
        advertise.setCreatetime(advertiseDB.getCreatetime());
        advertise.setUpdatetime(simpleDateFormat.format(new Date()));
        int res=advertiseMapper.updateByPrimaryKey(advertise);
        if (res==0){
            responseBo.setResMsg("更新失败");
            log.info("退出 AdminServiceImpl 类的 updateUser 方法");
            return responseBo;
        }
        responseBo.setResMsg("更新成功");
        log.info("退出 AdminServiceImpl 类的 updateUser 方法");
        return responseBo;
    }

}
